package com.example.mqexamples.custom.mq;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Description TODO
 * @Author benjamin
 * @Date 2020/1/21 10:14
 * @Version
 */
public class MqClient {

    public static void produce(String msg) throws IOException {


        try (Socket socket = new Socket(InetAddress.getLocalHost(), BrokerServer.SERVER_PORT);

             PrintWriter out = new PrintWriter(socket.getOutputStream());) {
            out.println(msg);
            out.flush();
        }
    }


    public static String consume() throws IOException {
        try (Socket socket = new Socket(InetAddress.getLocalHost(), BrokerServer.SERVER_PORT);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             PrintWriter out = new PrintWriter(socket.getOutputStream());

        ) {


            out.println("CONSUME");
            out.flush();

            String msg = in.readLine();

            return msg;
        }

    }



}
